package org.edu.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import org.edu.model.Address;
import org.edu.model.Faculity;
import org.edu.model.Sciency;
import org.edu.model.Student;
import org.edu.model.Teacher;
import org.edu.model.User;
import org.edu.service.StudentService;
import org.edu.service.UserService;
import org.primefaces.event.FlowEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.SelectableDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
@SessionScoped
public class UserController extends ListDataModel<User> implements Serializable, SelectableDataModel<User> {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient UserService userService;
	@Autowired
	private transient StudentService studentService;
	private boolean skip;
	private static Logger logger = Logger.getLogger(UserController.class
			.getName());
	private User user;
	private Address address;
	private Student student;
	private Teacher teacher;
	private List<User> userList;
	private List<Student> studentList;
	private List<User> filterUser;
	private List<Faculity> faculities;
	private List<Sciency> sciencies;
	private boolean disabledTeacher = false;
	private boolean disabledStudent = false;
	private boolean disabledAdmin = false;
	private static final List<SelectItem> rolesList = new ArrayList<>();

	public UserController() {
		user = new User();
	}

	@PostConstruct
	public void init() {
		userList = userService.users();
		studentList = studentService.students();
	}

	public String saveStudent() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			user.setRecord_date(new java.sql.Date(new java.util.Date()
					.getTime()));
			user.setStatus(true);
			user.setStudent(student);
			student.setUserStudent(user);
			user.setAddress(address);
			address.setUserAdress(user);
			userService.save(user);
			facesContext.addMessage(null, new FacesMessage("Save Student is Succesful"));
			init();
			user = new User();
			address = new Address();
			student = new Student();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}

	public String saveTeacher() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			user.setRecord_date(new java.sql.Date(new java.util.Date().getTime()));
			user.setTeacher(teacher);
			teacher.setUserTeacher(user);
			user.setAddress(address);
			address.setUserAdress(user);
			userService.save(user);
			address = new Address();
			teacher = new Teacher();
			facesContext.addMessage(null, new FacesMessage(
					"Save Teacher is Succesful"));
			init();
			user = new User();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}

	public String Status() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			if (user.getStatus()) {
				user.setStatus(false);
			} else {
				user.setStatus(true);
			}
			userService.update(user);
			facesContext.addMessage(null, new FacesMessage(
					"User Status Changed"));
			init();
			user = new User();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}

	public User getUser() {
		if (user == null) {
			user = new User();
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public Address getAddress() {
		if (address == null) {
			address = new Address();
		}
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Student getStudent() {
		if (student == null) {
			student = new Student();
		}
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Teacher getTeacher() {
		if (teacher == null) {
			teacher = new Teacher();
		}
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public List<User> getFilterUser() {
		return filterUser;
	}

	public List<Sciency> getSciencies() {
		return sciencies;
	}

	public void setSciencies(List<Sciency> sciencies) {
		this.sciencies = sciencies;
	}

	public List<Faculity> getFaculities() {
		return faculities;
	}

	public void setFilterUser(List<User> filterUser) {
		this.filterUser = filterUser;
	}

	public void onRowSelect(SelectEvent event) {
		FacesMessage msg = new FacesMessage("User Selected",
				((User) event.getObject()).getUser_number());
		;
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	@Override
	public Object getRowKey(User object) {
		return user.getUser_number();
	}

	@Override
	public User getRowData(String rowKey) {
		for (User user : userList) {
			if (user.getUser_number().equals(rowKey))
				return user;
		}
		return null;
	}

	public boolean isSkip() {
		return skip;
	}

	public List<Student> getStudentList() {
		return studentList;
	}

	public void setSkip(boolean skip) {
		this.skip = skip;
	}

	public boolean isDisabledTeacher() {
		return disabledTeacher;
	}

	public void setDisabledTeacher(boolean disabledTeacher) {
		this.disabledTeacher = disabledTeacher;
	}

	public boolean isDisabledStudent() {
		return disabledStudent;
	}

	public void setDisabledStudent(boolean disabledStudent) {
		this.disabledStudent = disabledStudent;
	}

	public boolean isDisabledAdmin() {
		return disabledAdmin;
	}

	public void setDisabledAdmin(boolean disabledAdmin) {
		this.disabledAdmin = disabledAdmin;
	}

	public String onFlowProcess(FlowEvent event) {
		logger.info("Current User step:" + event.getOldStep());
		logger.info("Next step:" + event.getNewStep());

		if (skip) {
			skip = false;
			return "confirm";
		} else {
			return event.getNewStep();
		}
	}

	public static List<SelectItem> getRoleslist() {
		return rolesList;
	}

}
